$(function() {
    //登录
    $("#login").click(function() {
        //获取username
        var username = $("#login_username").val();
        if (username == undefined || username == "") {
            layer.alert('请输入用户名！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        //获取paswd
        var passwd = $("#login_passwd").val();
        if (passwd == undefined || passwd == "") {
            layer.alert('请输入密码！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        var dateparam = {
            "username": username,
            "password": passwd
        };
        $.ajax({
            url: "/user/login",
            type: 'POST',
            // async:false,
            data: dateparam,
            beforeSend: function() {
                $('#login').attr("value", "登录中...");
            },
            success: function(data, status, xhr) {
                console.log(xhr.getResponseHeader("x-auth-token"));
                $.cookie('sycs', xhr.getResponseHeader("x-auth-token"),{path: '/' });
                alert("登录成功")
            },
            error: function(xhr, status, data) {
                //将错误信息取出来
                var responseText = $.evalJSON(xhr.responseText);
                layer.alert(responseText.message + '！', {
                    icon: 2,
                    shadeClose: false
                });
                return false;
            }
        });
    });
    /**
     验证用户名是否注册
     */
    $("#regist_username").blur(function() {
        var username = $("#regist_username").val();
        if (username != undefined && username != "") {
            //请求后台接口
            var dateparam = {
                "username": username
            };
            $.ajax({
                url: ROOTBASE + "user/checkusername",
                type: 'GET',
                // async:false,
                data: dateparam,
                success: function(data, status, xhr) {
                    // console.log(data.sucflag);
                    if (data.sucflag) {
                        $("#username_div").attr("class", "form-group success");
                    } else {
                        $("#username_div").attr("class", "form-group error");
                        $("#username_div").find("i[name='infoicon']").html("&#xe605;");
                        $("#username_div").find("b[name='infoconet']").html("用户名已经存在!");
                    }

                    // alert(data);
                },
                error: function(xhr, status) {
                    //将错误信息取出来
                    // var responseText = $.evalJSON(xhr.responseText);
                    // alert(responseText.message);
                    $("#username_div").attr("class", "form-group error");
                    $("#username_div").find("i[name='infoicon']").html("&#xe605;");
                    $("#username_div").find("b[name='infoconet']").html("用户名已经存在!");
                }
            });
        }
    });
    /**
     验证邮箱是否注册
     */
    $("#regist_email").blur(function() {
        var email = $("#regist_email").val();
        if (email != undefined && email != "") {
            //请求后台接口
            var dateparam = {
                "email": email
            };
            $.ajax({
                url: ROOTBASE + "user/checkemail",
                type: 'GET',
                // async:false,
                data: dateparam,
                success: function(data, status, xhr) {
                    if (data.sucflag) {
                        //未注册
                        $("#email_div").attr("class", "form-group success");
                    } else {
                        //已注册
                        $("#email_div").attr("class", "form-group error");
                        $("#email_div").find("i[name='infoicon']").html("&#xe605;");
                        $("#email_div").find("b[name='infoconet']").html("该邮箱已经注册!");
                    }
                },
                error: function(xhr, status) {
                    //将错误信息取出来
                    // var responseText = $.evalJSON(xhr.responseText);
                    // alert(responseText.message);
                    $("#email_div").attr("class", "form-group error");
                    $("#email_div").find("i[name='infoicon']").html("&#xe605;");
                    $("#email_div").find("b[name='infoconet']").html("该邮箱已经注册!");
                }
            });
        }
    });
    //注册
    registuser = function() {
        //获取用户名
        //6-20 英文或数字
        var username = $("#regist_username").val();
        if (username == undefined || username == "") {
            layer.alert('请输入用户名！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        } else {
            if (username.length < 6 || username.length > 20) {
                layer.alert('用户名长度必须在6-20位之间！', {
                    icon: 2,
                    shadeClose: false
                });
                return false;
            }
        }
        //邮箱验证
        var email = $("#regist_email").val();
        if (email == undefined || email == "") {
            layer.alert('请输入邮箱地址！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        var emailcheck = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!emailcheck.test(email)) {
            layer.alert('请输入正确的邮箱格式！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        //获取登录密码
        //
        var passwd = $("#regist_passwd").val();
        if (passwd == undefined || passwd == "") {
            layer.alert('请输入密码！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        } else {
            if (passwd.length < 6 || passwd.length > 20) {
                layer.alert('密码长度必须在6-20位之间！', {
                    icon: 2,
                    shadeClose: false
                });
                return false;
            }
        }
        //重复密码
        var repeat_passwd = $("#repeat_regist_passwd").val();
        if (repeat_passwd == undefined || repeat_passwd == "") {
            layer.alert('请输入重复密码！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        if (repeat_passwd != passwd) {
            layer.alert('两次密码不一致！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        if (!$('#isread').is(':checked')) {
            layer.alert('请勾选同意条款！', {
                icon: 2,
                shadeClose: false
            });
            return false;
        }
        //请求后台接口
        var dateparam = {
            "username": username,
            "passwd": passwd,
            "email": email
        };
        $.ajax({
            url: ROOTBASE + "user/regist",
            type: 'POST',
            async: false,
            data: dateparam,
            beforeSend: function() {
                $('#butRequestGo').attr("value", "正在注册...");
            },
            success: function(data, status, xhr) {
                if(data.sucflag) {
                    window.location.href = "login.html";
                }

                $("#email-active").removeClass("hide").siblings(".loginBox").addClass("hide");
                // window.location.href = "user/UserCenter.html";
            },
            error: function(xhr, status) {
                //将错误信息取出来
                var responseText = $.evalJSON(xhr.responseText);
                alert(responseText.message);
            }
        });

    }
    //登出
    logout = function() {

    }
    //注册
    $("#butrequest").click(function() {
        $("#userrequest").removeClass("hide").siblings(".loginBox").addClass("hide");
    });
    //返回登录
    $("#returnlogin").click(function() {
        $("#userlogin").removeClass("hide").siblings(".loginBox").addClass("hide");
    });
    //立即注册
    $("#butrequestgo").click(function() {
        //注册按钮
        registuser();
    });
    //忘记密码提交
    $("#usersubmit").click(function() {
        $("#email-active").removeClass("hide").siblings(".loginBox").addClass("hide");
    });
    //忘记密码
    $("#forgetpassword").click(function() {
        $("#forgetpassword-box").removeClass("hide").siblings(".loginBox").addClass("hide");
    });
})